<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Name:  Students Model
*
* Author:  Ervin R. Musngi
*
*/

class Student_m extends DataMapperExt
{
	var $table = 'students';
	
	var $has_many 	= array(
					'sections'	=> array(
						'class'			=> 'section_listing_m',
						'other_field'	=> 'students',
						'join_self_as'	=> 'student',
						'join_other_as'	=> 'section_listing',
						'join_table'	=> 'student_listings'
					)
	);

	var $has_one 	= array(
					'school_level_m' 	=> array(
						'class'			=> 'school_level_m',
						'other_field'	=> 'students',
						'join_self_as' 	=> 'student',
						'join_other_as' => 'school_level',
						'join_table'	=> 'student_listings'	
					),
					'school_year'	=> array(
						'class'			=> 'school_year_m',
						'other_field'	=> 'students',
						'join_self_as'	=> 'student',
						'join_other_as'	=> 'school_year',
						'join_table'	=> 'student_listings'
					)
				);	
	public function __construct($id = NULL)
	{
		parent::__construct($id);
	}
	
	public function get_active_student_count($limit, $offset)
	{
		$student = new Student_m();
			
		$total_rows = $student->where('is_active', '1')->get($limit, $offset)->count();
		
		return $total_rows;
	}
	
	public function get_all_active_student($limit, $offset)
	{
		$student = new Student_m();
		
		$student->where('is_active', '1')
				->get($limit, $offset);
				
		if ($student->result_count() > 0)
		{
			return $student;
		}
	}
	
	public function get_all( $id = 0 , $school_year_id = 0 )
	{
		
		$school_year = new School_year_m();
			
		$section = $school_year->where('id',$school_year_id)->get()->section_listings->get();
		
		$ids = array(0);
		foreach ( $section as $s )
		{
			foreach( $s->students->get() as $std )
			{
				$ids[] = $std->id;
			}
		}
		

		$student = new Student_m();
		
		$student->where('is_active',1)->where_not_in('id',$ids)->get();
		
		return $student;
		
	}
	
	public function get_current_student( $section_list_id = 0 )
	{
		$student = new Student_m();
		
		$student->where('is_active',1)->where_related('sections','id',$section_list_id)->get();
		
		return $student;
	}
	
	
	public function list_students( $section_list_id = 0 , $students = array() )
	{
				
		$stds = new Student_m();
		
		$stds->where_in('id',$students)->get();
		
		$ss = new Section_listing_m();
		
		$ss->where('id',$section_list_id)->get();
		
		if ( is_null($ss->id) || $ss->result_count() == 0)
		{
			return false;
		}
		
		$student_listing = new Student_listing_m();
		
		$student_listing->where('section_listing_id',$section_list_id)->get()->delete_all();
		
		foreach ( $stds as $s )
		{
			$data = array(
				'student_id' 			=> $s->id,
				'section_listing_id'	=> $ss->id
			);
			
			$this->db->insert('student_listings',$data);
		}
		
		return true;
		
	}
	
	public function check_student_number($std_num)
	{
		
		$student = new Student_m();
		
		$student->where('number',$std_num)->get(1);
		

		
		if ( $student->result_count() > 0 )
		{
			return false;
		}
		else
		{
			return true;
		}
		
	}
	
}